package com.dianping.cat.home.alert.summary.transform;

import com.dianping.cat.home.alert.summary.entity.Alert;
import com.dianping.cat.home.alert.summary.entity.AlertSummary;
import com.dianping.cat.home.alert.summary.entity.Category;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;





public class DefaultDomParser
  extends Object
  implements IParser<Node>
{
  protected Node getChildTagNode(Node parent, String name) {
/*  26 */     NodeList children = parent.getChildNodes();
/*  27 */     int len = children.getLength();

    for (int i = 0; i < len; i++) {
/*  30 */       Node child = children.item(i);

      if (child.getNodeType() == 1 &&
/*  33 */         child.getNodeName().equals(name)) {
/*  34 */         return child;
      }
    }


/*  39 */     return null;
  }

  protected List<Node> getChildTagNodes(Node parent, String name) {
/*  43 */     NodeList children = parent.getChildNodes();
/*  44 */     int len = children.getLength();
/*  45 */     List<Node> nodes = new ArrayList<Node>(len);

/*  47 */     for (int i = 0; i < len; i++) {
/*  48 */       Node child = children.item(i);

/*  50 */       if (child.getNodeType() == 1 && (
/*  51 */         name == null || child.getNodeName().equals(name))) {
/*  52 */         nodes.add(child);
      }
    }


/*  57 */     return nodes;
  }

  protected Node getDocument(String xml) {
/*  61 */     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

/*  63 */     dbf.setIgnoringElementContentWhitespace(true);
/*  64 */     dbf.setIgnoringComments(true);

    try {
/*  67 */       DocumentBuilder db = dbf.newDocumentBuilder();

/*  69 */       return db.parse(new InputSource(new StringReader(xml)));
/*  70 */     } catch (Exception x) {
/*  71 */       throw new RuntimeException(x);
    }
  }

  protected List<Node> getGrandChildTagNodes(Node parent, String name) {
/*  76 */     Node child = getChildTagNode(parent, name);
/*  77 */     NodeList children = (child == null) ? null : child.getChildNodes();
    int len = (children == null) ? 0 : children.getLength();
/*  79 */     List<Node> nodes = new ArrayList<Node>(len);

/*  81 */     for (int i = 0; i < len; i++) {
/*  82 */       Node grandChild = children.item(i);

/*  84 */       if (grandChild.getNodeType() == 1) {
/*  85 */         nodes.add(grandChild);
      }
    }

/*  89 */     return nodes;
  }


/*  93 */   public AlertSummary parse(Node node) { return parse(new DefaultDomMaker(), new DefaultLinker(false), node); }


  public AlertSummary parse(String xml) throws SAXException, IOException {
/*  97 */     Node doc = getDocument(xml);
/*  98 */     Node rootNode = getChildTagNode(doc, "alert-summary");

/* 100 */     if (rootNode == null) {
/* 101 */       throw new RuntimeException(String.format("alert-summary element(%s) is expected!", new Object[] { "alert-summary" }));
    }

/* 104 */     return parse(new DefaultDomMaker(), new DefaultLinker(false), rootNode);
  }

  public AlertSummary parse(IMaker<Node> maker, ILinker linker, Node node) {
/* 108 */     AlertSummary alertSummary = maker.buildAlertSummary(node);

/* 110 */     if (node != null) {
/* 111 */       AlertSummary parent = alertSummary;

/* 113 */       for (Node child : getChildTagNodes(node, "category")) {
/* 114 */         Category category = maker.buildCategory(child);

/* 116 */         if (linker.onCategory(parent, category)) {
/* 117 */           parseForCategory(maker, linker, category, child);
        }
      }
    }

/* 122 */     return alertSummary;
  }


  public void parseForAlert(IMaker<Node> maker, ILinker linker, Alert parent, Node node) {}

  public void parseForCategory(IMaker<Node> maker, ILinker linker, Category parent, Node node) {
/* 129 */     for (Node child : getChildTagNodes(node, "alert")) {
/* 130 */       Alert alert = maker.buildAlert(child);

/* 132 */       if (linker.onAlert(parent, alert))
/* 133 */         parseForAlert(maker, linker, alert, child); 
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/alert/summary/transform/DefaultDomParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */